#!/usr/bin/env python
import sys

DIR_MASK = 0b10000000
RECIPIENT_MASK = 0b00011111
TYPE_MASK = 0b01100000

direction_decode_table = {0x00:'out', 0x80:'in'}
recipient_decode_table = ['device', 'interface', 'endpoint', 'other', 'reserved']
transfer_type_decode_table = ['standard', 'class', 'vendor', 'reserved']

if len(sys.argv) < 2:
    print ('Usage: decode-request-type <request-type>')
    sys.exit(1)

request_type = int(sys.argv[1], 0)

direction = request_type & DIR_MASK
recipient = request_type & RECIPIENT_MASK
transfer_type = (request_type & TYPE_MASK) >> 5

if recipient > len(recipient_decode_table)-1:
    recipient = len(recipient_decode_table) - 1

print ('direction=%s, type=%s, recipient=%s' % \
        (direction_decode_table[direction],
         transfer_type_decode_table[transfer_type],
         recipient_decode_table[recipient]))


